package xpn.platform.modules.todo.todolink;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.rest.core.annotation.HandleBeforeCreate;
import org.springframework.data.rest.core.annotation.HandleBeforeLinkDelete;
import org.springframework.data.rest.core.annotation.RepositoryEventHandler;
import xpn.platform.modules.sys.user.SysUser;
import xpn.platform.modules.sys.user.SysUserDAO;
import xpn.platform.modules.todo.wsdl.TodosCall;

import lombok.extern.slf4j.Slf4j;

/**
 * 待办 实体  事件监听
 *
 * @author zyk
 * @version 2018/03/22 14:26
 */
@Slf4j
@RepositoryEventHandler(TodoLink.class)
public class TodoLinkEventHandler {

    @Autowired
    SysUserDAO sysUserDAO;




//    @Autowired
//    TodosCall todosCall;

    @HandleBeforeCreate
    public void handleBeforeCreate(TodoLink todoLink) {
        SysUser sender = sysUserDAO.getOne(Long.parseLong(todoLink.getSenderId()));
        String senderFullName = sender.getName() +"/"+ sender.getOrg().fetchFullName();
        SysUser sendee = sysUserDAO.getOne(Long.parseLong(todoLink.getSendeeId()));
        /*try {
            todosCall.create(todoLink.getLinkUrl(), sender.getUserCode(), senderFullName, todoLink.getSubject(), todoLink.getCategory(), todoLink.getEmergency() + "", sendee.getUserCode());
            log.info("连接多米诺平台创建TODO[" + todoLink.getSubject() + "]成功");
        } catch (RemoteException | UnsupportedEncodingException e) {
            e.printStackTrace();
            log.error("连接多米诺平台创建TODO[" + todoLink.getSubject() + "]失败", e);
        }*/
    }

    @HandleBeforeLinkDelete
    public void beforeLinkDelete(TodoLink todoLink) {
        SysUser sendee = sysUserDAO.getOne(Long.parseLong(todoLink.getSendeeId()));
        /*try {
            todosCall.delete(todoLink.getLinkUrl(), sendee.getUserCode());
            log.info("LinkDelete_连接多米诺平台删除TODO成功");
        } catch (RemoteException | UnsupportedEncodingException e) {
            e.printStackTrace();
            log.error("LinkDelete_连接多米诺平台删除TODO失败", e);
        }*/
    }
}
